﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace CCF20Model
{
    partial class MaintenanceType : IDataErrorInfo
    {
        
        public MaintenanceType()
        {
            this.Priority = 1;
            
            CCF20Entities ccf20Ent;
            ccf20Ent = new CCF20Entities();

            //为了避免与智信通自己的保养项目混合，客户定制的保养项目从1百万开始
            this.AnchiID = ccf20Ent.MaintenanceTypes.Max(m => m.AnchiID) + 1;
            if (this.AnchiID < 1000000)
            {
                this.AnchiID = 1000000;
            }
        }

        partial void OnServiceMileageChanged()
        {
            this.RemoveError("ServiceMileage");

            if (this.ServiceMileage == null)
            {
                this.AddError("ServiceMileage", "服务里程不可为空");

            }
        }
        
        partial void OnServicePeriodChanged()
        {
            this.RemoveError("ServicePeriod");

            if (this.ServicePeriod == null)
            {
                this.AddError("ServicePeriod", "服务周期不可为空");

            }
        }
        

        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion
    }
}
